//
//  YUYAHudProgressView.m
//  com.yigu.yuya
//
//  Created by hxp on 16/3/4.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIHudProgressView.h"
@implementation YAYIHudProgressView
-(id)initWithView:(UIView *)view
{
    self = [super initWithFrame:view.frame];
    if (self ) {
        _rect = view.frame;
        [self createControl];
        [self createMsgView];
        [self createMsgLabel];
    }
    return self;
}
-(void)createMsgView{
    if (self.msgView == nil) {
        self.msgView = [UIView new];
        self.msgView.backgroundColor = [UIColor  colorWithRed:40/255.0 green:40/255.0 blue:40/255.0 alpha:0.6];
        self.msgView.clipsToBounds = YES;
        self.msgView.layer.shadowOpacity = 1;
        self.msgView.layer.shadowOffset = CGSizeMake(1, 1);
        self.msgView.layer.cornerRadius = 3;
        [self addSubview:self.msgView];
    }
    [self.msgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(165, 120));
    }];

}
-(void)createMsgLabel{
    if (self.msgLabel == nil) {
        self.msgLabel = [UILabel new];
        self.msgLabel.textColor = [UIColor whiteColor];
        self.msgLabel.font = [UIFont systemFontOfSize:14];
        self.msgLabel.textAlignment = NSTextAlignmentCenter;
        [self.msgView addSubview:self.msgLabel];
    }
    [self.msgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.msgView);
        make.centerY.equalTo(self.msgView);
        make.size.mas_equalTo(CGSizeMake(165, 20));
    }];
   
}
-(void)setMessage:(NSString *)msg
{
    self.msgLabel.text = msg;
    self.msgLabel.numberOfLines = 0;
    CGFloat height = [self heightForRow:msg font:[UIFont systemFontOfSize:16]];
    [self.msgLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.msgView);
        make.size.mas_equalTo(CGSizeMake(160, height));
    }];

}
-(void)createControl{
     self.control = [[UIControl  alloc] initWithFrame:_rect];
     self.control.backgroundColor = [UIColor lightGrayColor];
     self.control.alpha = 0.2;
     [self addSubview:self.control];
}
-(void)removeFromSuperview:(BOOL)isRemove time:(NSInteger)t isHinden:(BOOL)ishidden
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(t * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (ishidden) {
            self.msgLabel.hidden = YES;
            self.msgView.hidden = YES;
            self.control.hidden = YES;
            self.hidden = YES;
        }
        if (isRemove) {
            [self.msgLabel removeFromSuperview];
            [self.msgView removeFromSuperview];
            [self.control removeFromSuperview];
            [self removeFromSuperview];
        }
    });
}
-(CGFloat)heightForRow:(NSString *)str font:(UIFont *)font
{
    CGSize  size = [str  sizeWithFont:font constrainedToSize:CGSizeMake(160, MAXFLOAT) lineBreakMode:NSLineBreakByCharWrapping];
    return size.height;
}

@end
